package com.nsoft.remotemedia.business;

import java.util.ArrayList;
import java.util.List;

import com.nsoft.remotemedia.objects.VideoItem;
import com.nsoft.remotemedia.utils.Constant;
import com.nsoft.remotemedia.utils.U2bUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class YoutubeLoader extends AsyncTask<Void, Integer, Void>{
	private final int VAL_LOAD_START=0;
	private final int VAL_LOAD_SERVICE_DONE=30;
	private final int VAL_LOAD_MOST_POPULAR_DONE=100;
	
	List<VideoItem> u2bList;
	
	private ProgressDialog progressor=null;
	private YoutubeLoadListener loadListener;
	private String searchStr;
	
	public YoutubeLoader(Context ctx,String searchStr){
		this.progressor=new ProgressDialog(ctx);
		this.u2bList=new ArrayList<VideoItem>();
		this.searchStr=searchStr;
	}
	
	public void setLoadListener(YoutubeLoadListener listener){
		this.loadListener=listener;
	}
	
	@Override
	protected void onPreExecute() {
		progressor.setCancelable(true);
		progressor.setMessage(Constant.u2b.LOADING);
		progressor.setProgressStyle(
				ProgressDialog.STYLE_SPINNER);
		progressor.setProgress(0);
		progressor.setMax(100);
		progressor.setProgress(0);
		progressor.show();
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		publishProgress(VAL_LOAD_START);
		U2bUtils.startUp();
		publishProgress(VAL_LOAD_SERVICE_DONE);
		try{
			if (this.searchStr.equals(Constant.EMPTY)){
				u2bList=U2bUtils.getMostPopularFeeds();
			}else{
				u2bList=U2bUtils.getFeeds(searchStr);
			}
		}catch(Exception e){}
		publishProgress(VAL_LOAD_MOST_POPULAR_DONE);
		return null;
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		if (values[0]==VAL_LOAD_SERVICE_DONE){
			
		}else if (values[0]==VAL_LOAD_MOST_POPULAR_DONE){
			loadListener.updateU2bFeeds(u2bList);
			progressor.dismiss();
		}
	}
}
